﻿namespace App

open System
open System.Windows
open System.Windows.Controls
open System.Windows.Markup
open System.Windows.Input
open App.Controls
open App.Commands

type MainWindow() as self =
    inherit Window(Title = "F# WPF Template")

    [<DefaultValue>]
    val mutable rootLayout : Grid

    [<DefaultValue>]
    val mutable themeMenuItem : MenuItem

    [<DefaultValue>]
    val mutable mainGrid : Grid

    let changeTheme (item:obj) =
        match item with
        | :? MenuItem as m->
            self.themeMenuItem.Items
                |> Seq.cast 
                |> Seq.filter (fun (x:MenuItem) -> not (x = m))
                |> Seq.iter (fun x -> x.IsChecked <- false)
            if (m.Name.Equals("Default")) then
                Application.Current.Resources.MergedDictionaries.Clear()
            else                    
                let source =  new Uri("/App;component/Themes/" + m.Name + ".xaml",  UriKind.RelativeOrAbsolute)
                let skin = new ResourceDictionary(Source = source)
                Application.Current.Resources.MergedDictionaries.Clear()
                Application.Current.Resources.MergedDictionaries.Add(skin)
        | _ -> ()

    let exitCommand = new RoutedUICommand("Exit", "Exit", typeof<MainWindow>, InputGestureCollection([| KeyGesture(Key.F4, ModifierKeys.Alt) |]))         
    let aboutCommand = new RoutedUICommand("About", "About", typeof<MainWindow>)         
    let themeCommand = new DelegateCommand(changeTheme)

    let makeCommandBinding (command:#ICommand, execute :ExecutedRoutedEventHandler, canExecute : CanExecuteRoutedEventHandler) =
        self.CommandBindings.Add(new CommandBinding(command, execute, canExecute)) |> ignore
        
    do  
        self.rootLayout <- Application.LoadComponent(new System.Uri("/App;component/RootLayout.xaml", UriKind.Relative)) :?> Grid  
        self.mainGrid <- self.rootLayout.FindName("mainGrid") :?> Grid
        self.mainGrid.Children.Add(new SampleControl()) |> ignore
        self.themeMenuItem <- self.rootLayout.FindName("ThemeMenu") :?> MenuItem  
        self.rootLayout.DataContext <- self
        self.Content <- self.rootLayout                   
        self.BindCommands()
                                        
    member private self.BindCommands() =
        makeCommandBinding(exitCommand,
                ExecutedRoutedEventHandler(fun _ _ -> self.Close()),                    
                CanExecuteRoutedEventHandler(fun _ e -> e.CanExecute <- true ))
        makeCommandBinding(aboutCommand,
                ExecutedRoutedEventHandler(fun _ _ -> MessageBox.Show("WPF Template", "About") |> ignore),                    
                CanExecuteRoutedEventHandler(fun _ e -> e.CanExecute <- true ))         
              
    member self.ExitCommand with get() = exitCommand
    member self.AboutCommand with get() = aboutCommand
    member self.ThemeCommand with get() = themeCommand